#include "messenger.h"
#include "confirmdialog.h"
#include "reporteditdialog.h"
Messenger::Messenger(DbConnection* Db, QWidget *Parent) :
    QDialog(Parent), m_db(Db)
{
    QVBoxLayout* main = new QVBoxLayout;
    table = new vTable(this);
    main->setContentsMargins(10,10,10,10);
    main->addWidget(table);
    setLayout(main);
    connect(table,SIGNAL(cellDoubleClicked(int,int)),this,SLOT(onCellDoubleClicked(int,int)));
    connect(table,SIGNAL(cellActivated(int,int)),this,SLOT(onCellActivated(int,int)));
    connect(this,SIGNAL(messageConfirmed(int,int,QIcon)),table,SLOT(set_Icon(int,int,QIcon)));
    setFixedHeight(sizeHint().height()+300);
    setFixedWidth(sizeHint().width()+340);
    setModal(true);
    table->setSize(m_messages.count(),3);
    table->setColSettings("5%;75%;20%");
    table->setHorizontalHeaderLabels(QStringList()<<tr("№")<<tr("Содержание")<<tr("Дата получения"));
    setWindowTitle(tr("Сообщения"));
    setWindowIcon(QIcon(":/messenger"));
}
void Messenger::readMessages()
{
    blockSignals(true);
    loadMessages();
    //m_messages.clear();
    //loadMessages();
    blockSignals(false);
    show();
}

void Messenger::loadMessages()
{
   DbConnection::QueryResult res = m_db->execute("select * from =schema=.ex_messages");
   if(res.success())
   {
        int count=0;
        for(int i=0;i<res.rows();i++)
        {
            Message message;
            message.id=res.at(i,0).toInt();
            message.text = res.at(i,1).toString();
            message.fid = res.at(i,2).toString();
            message.date = res.at(i,3).toDateTime();
            bool ok=true;
            int i=0;
            while(ok&&i<m_messages.count())
            {
                ok=(m_messages.at(i).id!=message.id);
                i++;
            }
            if(ok)
            {
                message.reminder=res.at(i,4).isNull();
                if(!message.reminder&&res.at(i,4).toBool())
                {
                   message.confirmed=m_db->execute(tr("select =schema=.ex_confirm_message(%1)").arg(message.id)).success();
                }
                else
                {
                   message.confirmed=message.reminder;
                   if(message.reminder)
                       m_db->execute(tr("update =schema=.confirmations set state=22 where eid=%1").arg(message.id));
                }
                m_messages.append(message);
                count++;
            }
        }
        if(count>0)
        {
            emit newMessages(gcm(count));
            table->setSize(m_messages.count(),3);
            table->setColSettings("5%;75%;20%");
            table->setHorizontalHeaderLabels(QStringList()<<tr("№")<<tr("Содержание")<<tr("Дата получения"));
            for(int i=0;i<table->rowCount();i++)
            {
                table->setCell(i,1,m_messages.at(i).text);
                table->setCell(i,0,"",m_messages.at(i).confirmed?QIcon(":/mconfirm"):QIcon(":/mnew"));
                table->setCell(i,2,m_messages.at(i).date);
            }
        }
   }
}

void Messenger::onCellDoubleClicked(const int &row, const int &col)
{
    hide();
    emit messageDoubleClicked(m_messages.at(row).fid);
}

void Messenger::onCellActivated(const int &row, const int &col)
{
    if(m_messages.at(row).reminder||m_messages.at(row).confirmed) return;
    ConfirmDialog dialog(m_db,this);
    dialog.exec();
    if(dialog.result()==QDialog::Accepted)
       if(m_db->execute(tr("select =schema=.ex_confirm_message(%1)").arg(m_messages.at(row).id)).success())
           setConfirmed(row);
}
void Messenger::setConfirmed(const int &Index)
{
    m_messages[Index].confirmed=true;
    emit messageConfirmed(Index,0,QIcon(":/mconfirm"));
}

QString Messenger::gcm(const int &count) const
{
    if((count>9)&&(count<21))return tr("Получено %1 сообщений").arg(count);
    int var=count%10;
    if(var==1)return tr("Получено %1 сообщение").arg(count);
    if((var>1)&&(var<5))return tr("Получено %1 сообщения").arg(count);
    return tr("Получено %1 сообщений").arg(count);
}

